This video talks about different methods to create multidimensional arrays in C++
Codes:
This video talks about challenges we face when we pass multidimensional arrays as arguments.
Codes:



5 2 -10 5
2008101287 4195777
Size of integer in this compiler is 4
Address arr[0] is 0x7ffd636b4260
Address arr[1] is 0x7ffd636b4264
Address arr[2] is 0x7ffd636b4268
Address arr[3] is 0x7ffd636b426c
Address arr[4] is 0x7ffd636b4270
Multi-dimensional Arrays
//creates (size1 * size2) type 2-D matrixA 2-d array and a 3-d array can be visualized as:
int arr[size1][size2]
//creates (size1 * size2 * size3) type 2-D matrix
int arr[size1][size2][size3]
...
We can continue with as many dimensions as we like:
// (size1 * size2 * ... *size_n) matrix
int arr[size1][size2][size3]....[size_n]


arr[i][j];
arr[i][j][k];
C-style (character arrays and literals)
C-style strings are defined using character arrays. There is an extra character called the null (\0) character appended to the character array to mark it's end.char str_name[size];
char str[size];Below is the memory representation of a string "Geeks".
char str[] = "Geeks";
char str[5] = "Geeks";
char str[] = {'G','e','e','k','s'};

Input: Hello
Output: Hello
HELLO
destinationsource
-100 <0 indicating str1 appears before str2
1 >0 indicating str1 appears after str3
1 >0 indicating str2 appears after str3
Source: Hello World
Destination: Hello World
First occurrence of string 'for'
in 'GeeksforGeeks' is 'forGeeks'
String Class in C++ STL
| C-style character array | C++ string class |
|---|---|
| Array of characters terminated by null (\0) | Class object instance storing stream of characters |
| Allocated Statically (can't be modified in run-time) | Allocated Dynamically (modifiable at run-time) |
| Vulnerable to array decay | Class-based implementation evades such vulnerabilities |
| Limited inbuilt functions for manipulation | Rich inbuilt function support for manipulation |
string str_name;
Input: Hello
Output: Hello
5
// Both of the below methods works fine:
Method 1:
string str = "This is a sample string"; // This is correct
Method 2:
string str;
str = "This is a sample string"; // This is also correct
GeeksforGeeks
Print 1st character: G
s
Length of String: 13
String is empty
Hello and Good Day to Everyone
abcd
abc
5
String 'abc' not found
for
forGeeks
12
+ 4 5Complete Code/Output
* 2 3
Q
- 5 4
#include <bits/stdc++.h>
using namespace std;
int main()
{
//opr ~ operation to perform
//op1 ~ operand 1
//op2 ~ operand 2
//OUTPUT: (op1 opr op2)
char opr;
int op1, op2;
while (true) {
cin >> opr;
if (opr == 'Q')
break;
else {
cin >> op1 >> op2;
switch(opr) {
case '+':
cout << op1 + op2 << endl;
break;
case '-':
cout << op1 - op2 << endl;
break;
case '*':
cout << op1 * op2 << endl;
break;
case '/':
cout << op1 / op2 << endl;
break;
default:
cout << "Invalid Operator\n";
break;
}
}
}
return 0;
}
9
6
4.16667
string kayak is a palindrome
string kappa is not a palindrome
11101
datatype *var_name;
int *ptr; //ptr can point to an address which holds int data

Value at ptr = 0x7ffdcf42ad9c
Value at var = 20
Value at *ptr = 20
References in C++
C++ References is a new language construct which was introduced to reduce the dependency over pointers for doing indirect memory access. Usage of pointers requires caution as the programmer needs to manually deallocate them to avoid memory leaks. A reference is an alternative name for a memory location. References are useful in following situations -3 2
Types of Function Call (References v/s Pointers)
There are 3 ways to pass C++ arguments to a function:
// C++ program to illustrate call-by-methods in C++
#include <bits/stdc++.h>
using namespace std;
// Pass-by-Value
int square1(int n)
{
// Address of n in square1() is not the same as n1 in main()
cout << "address of n1 in square1(): " << &n << "\n";
// clone modified inside the function
n *= n;
return n;
}
// Pass-by-Reference with Pointer Arguments
void square2(int* n)
{
// Address of n in square2() is the same as n2 in main()
cout << "address of n2 in square2(): " << n << "\n";
// Explicit de-referencing to get the value pointed-to
*n *= *n;
}
// Pass-by-Reference with Reference Arguments
void square3(int& n)
{
// Address of n in square3() is the same as n3 in main()
cout << "address of n3 in square3(): " << &n << "\n";
// Implicit de-referencing (without '*')
n *= n;
}
void geeks()
{
// Call-by-Value
int n1 = 8;
cout << "address of n1 in main(): " << &n1 << "\n";
cout << "Square of n1: " << square1(n1) << "\n";
cout << "No change in n1: " << n1 << "\n";
// Call-by-Reference with Pointer Arguments
int n2 = 8;
cout << "address of n2 in main(): " << &n2 << "\n";
square2(&n2);
cout << "Square of n2: " << n2 << "\n";
cout << "Change reflected in n2: " << n2 << "\n";
// Call-by-Reference with Reference Arguments
int n3 = 8;
cout << "address of n3 in main(): " << &n3 << "\n";
square3(n3);
cout << "Square of n3: " << n3 << "\n";
cout << "Change reflected in n3: " << n3 << "\n";
}
// Driver program
int main()
{
geeks();
}address of n1 in main(): 0x7ffec6732e4c
address of n1 in square1(): 0x7ffec6732e2c
Square of n1: 64
No change in n1: 8
address of n2 in main(): 0x7ffec6732e50
address of n2 in square2(): 0x7ffec6732e50
Square of n2: 64
Change reflected in n2: 64
address of n3 in main(): 0x7ffec6732e54
address of n3 in square3(): 0x7ffec6732e54
Square of n3: 64
Change reflected in n3: 64
Array Name as Pointers
An array name contains the address of first element of the array which acts like constant pointer. It means, the address stored in array name can't be changed.Elements of the array are: 5 10 20
If pointer ptr is sent to a function as an argument, the array val can be accessed in a similar fashion.Pointers and String literals
String literals are arrays containing null-terminated character sequences. String literals are arrays of type character plus terminating null-character, with each of the elements being of type const char (as characters of string can't be modified).const char * ptr = "geek";
This declares an array with the literal representation for "geek", and then a pointer to its first element is assigned to ptr. If we imagine that "geek" is stored at the memory locations that start at address 1800, we can represent the previous declaration as:
char x = *(ptr+3);
char y = ptr[3];
Here, both x and y contain k stored at 1803 (1800+3).Pointers to pointers
In C++, we can create a pointer to a pointer that in turn may point to data or other pointer. The syntax simply requires the unary operator (*) for each level of indirection while declaring the pointer.char a;
char *b;
char ** c;
a = ’g’;
b = &a;
c = &b;
Here b points to a char that stores ‘g’ and c points to the pointer b.Void Pointers
This is a special type of pointer available in C++ which represents absence of type. void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereferencing properties).
// C++ program to illustrate Void Pointer in C++
#include <bits/stdc++.h>
using namespace std;
void increase(void* data, int ptrsize)
{
if (ptrsize == sizeof(char)) {
char* ptrchar;
// Typecast data to a char pointer
ptrchar = (char*)data;
// Increase the char stored at *ptrchar by 1
(*ptrchar)++;
cout << "*data points to a char"
<< "\n";
}
else if (ptrsize == sizeof(int)) {
int* ptrint;
// Typecast data to a int pointer
ptrint = (int*)data;
// Increase the int stored at *ptrchar by 1
(*ptrint)++;
cout << "*data points to an int"
<< "\n";
}
}
void geek()
{
// Declare a character
char c = 'x';
// Declare an integer
int i = 10;
// Call increase function using a char and int address respectively
increase(&c, sizeof(c));
cout << "The new value of c is: " << c << "\n";
increase(&i, sizeof(i));
cout << "The new value of i is: " << i << "\n";
}
// Driver program
int main()
{
geek();
}*data points to a char
The new value of c is: y
*data points to an int
The new value of i is: 11
Invalid pointers
A pointer should point to a valid address but not necessarily to valid elements (like for arrays). These are called invalid pointers. Uninitialized pointers are also invalid pointers.int *ptr1;
int arr[10];
int *ptr2 = arr+20;
Here, ptr1 is uninitialized so it becomes an invalid pointer and ptr2 is out of bounds of arr so it also becomes an invalid pointer.NULL Pointers
Null pointer is a pointer which point nowhere and not just an invalid address.int *ptr1 = 0;
int *ptr2 = NULL;
VOID Pointers
A void pointer is a pointer that doesn't have any type and thus can point to any data-type by explicit casting. Since the data-type this pointer refers to isn't defined upon declaration, we can't dereference a void pointer.prog.cpp: In function 'int main()':All the assignment statements are valid because a void pointer can be made to point to any data-type. However, the above code doesn't compile because of the line:
prog.cpp:17:14: error: 'void*' is not a pointer-to-object type
cout << *p << endl;
^
NULL Pointers
A pointer declared a value always contains garbage value, thus pointing to a random location in memory. To signify a pointer as not pointing to any meaningful variable, we use the NULL Macro. NULL is a Macro defined in standard library, and it's value is generally implementation specific, but most compilers set it to value: 0. Thus, assigning NULL to a pointer, and putting it in an if-statement generates boolean-false (because of 0):0
Pointer is NULL
prog.cpp: In function 'int main()':Since NULL is 0, it may mean int as well as int*, causing ambiguity. To fix this, C++ introduced nullptr pointer-literal keyword. In modern C++ usage, thus we set a pointer to nullptr instead of NULL, when we declare a pointer pointing to nothing. Same above code with the call: fun(nullptr) will not generated any errors as ambiguity is removed.
prog.cpp:13:10: error: call of overloaded 'fun(NULL)' is ambiguous
fun(NULL); //ambiguity issue
^
prog.cpp:4:6: note: candidate: void fun(int)
void fun(int x) { cout << "Integer Call: " << x << endl; }
^
prog.cpp:5:6: note: candidate: void fun(int*)
void fun(int *x) { cout << "Pointer Call: " << x << endl; }
^
Value of a: 6
Value of b: 5
1 2 3 4
5 6 7 8
9 10 11 12
Asked In: Amazon